package com.csgc.camera.storage.base

import android.app.Application
import android.os.Parcelable
import com.csgc.wifi.storage.base.IPreferences
import com.tencent.mmkv.MMKV

open class BasePreferences(preferenceId: String) : IPreferences {

    private val mmkv = MMKV.mmkvWithID(preferenceId)

    companion object{
        fun init(application: Application){
            MMKV.initialize(application)
        }
    }
    override fun containsKey(key: String): Boolean {
        return mmkv.containsKey(key)
    }

    override fun removeValueForKey(key: String) {
        mmkv.removeValueForKey(key)
    }

    override fun getBoolean(key: String, defValue: Boolean?): Boolean? {
        return defValue?.let {
            mmkv.decodeBool(key, it)
        } ?: mmkv.decodeBool(key)
    }

    override fun setBoolean(key: String, value: Boolean) {
        mmkv.encode(key, value)
    }

    override fun getInt(key: String, defValue: Int?): Int? {
        return defValue?.let {
            mmkv.decodeInt(key, it)
        } ?: mmkv.decodeInt(key)
    }

    override fun setInt(key: String, value: Int) {
        mmkv.encode(key, value)
    }

    override fun getLong(key: String, defValue: Long?): Long? {
        return defValue?.let {
            mmkv.decodeLong(key, it)
        } ?: mmkv.decodeLong(key)
    }

    override fun setLong(key: String, value: Long) {
        mmkv.encode(key, value)
    }

    override fun getFloat(key: String, defValue: Float?): Float? {
        return defValue?.let {
            mmkv.decodeFloat(key, it)
        } ?: mmkv.decodeFloat(key)
    }

    override fun setFloat(key: String, value: Float) {
        mmkv.encode(key, value)
    }

    override fun getDouble(key: String, defValue: Double?): Double? {
        return defValue?.let {
            mmkv.decodeDouble(key, it)
        } ?: mmkv.decodeDouble(key)
    }

    override fun setDouble(key: String, value: Double) {
        mmkv.encode(key, value)
    }

    override fun getByteArray(key: String, defValue: ByteArray?): ByteArray? {
        return defValue?.let {
            mmkv.decodeBytes(key, it)
        } ?: mmkv.decodeBytes(key)
    }

    override fun setByteArray(key: String, value: ByteArray) {
        mmkv.encode(key, value)
    }

    override fun getString(key: String, defValue: String?): String? {
        return defValue?.let {
            mmkv.decodeString(key, it)
        } ?: mmkv.decodeString(key)
    }

    override fun setString(key: String, value: String) {
        mmkv.encode(key, value)
    }

    override fun getStringSet(key: String, defValue: Set<String>?): Set<String>? {
        return defValue?.let {
            mmkv.decodeStringSet(key, it)
        } ?: mmkv.decodeStringSet(key)
    }

    override fun setStringSet(key: String, value: Set<String>) {
        mmkv.encode(key, value)
    }

    override fun <T : Parcelable> getParcelable(key: String, tClass: Class<T>): T? {
        return mmkv.decodeParcelable(key, tClass)
    }

    override fun setParcelable(key: String, value: Parcelable) {
        mmkv.encode(key, value)
    }
}
